package controller

import (
	"github.com/gin-gonic/gin"
	"go_code/gin-blog/dao/db"
	"go_code/gin-blog/model"
	"go_code/gin-blog/service"
	"net/http"
	"strconv"
	"time"
)

func PostComment() gin.HandlerFunc {
	return func(c *gin.Context) {
		author := c.PostForm("author")
		content := c.PostForm("comment")
		articleId := c.PostForm("article_id")
		//log.Println(articleId)
		artId, _ := strconv.ParseInt(articleId, 10, 64)
		comment := &model.Comment{
			Content:     content,
			Username:    author,
			Article_id:  artId,
			Create_Time: time.Now(),
			Status:      1,
		}
		flag := db.InsertComment(comment)
		if !flag {
			c.HTML(http.StatusInternalServerError, "views/500.html", nil)
		}
		//返回到detail页面
		detail, err := service.GetArticleDetail(articleId)
		if err != nil {
			c.HTML(http.StatusInternalServerError, "500.html", nil)
		}
		category, err := service.GetAllCategoryList()
		if err != nil {
			c.HTML(http.StatusInternalServerError, "500.html", nil)
		}
		comment_list, err := service.GetCommentList(articleId)
		if err != nil {
			c.HTML(http.StatusInternalServerError, "500.html", nil)
		}
		//relative_article, err := service.Get
		c.HTML(http.StatusOK, "views/detail.html", gin.H{
			"detail":       detail,
			"category":     category,
			"comment_list": comment_list,
			//"relative_article": relative_article,
		})

	}
}
